// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  var {orderNum,type} = event
  if(type=='shouhuo')
  {
    await db.collection("jf_info").where({
      openid,
      orderNum
    }).update({
      data:{
        state:3
      }
    })
   price =  await db.collection("jf_info").where({
      openid,
      orderNum
    }).get()
    console.log(price.data[0].price)
    if(price.data[0].isusecard==false)//没用优惠券  抽3成   用优惠券不抽
    {
      price.data[0].price = price.data[0].price*0.7
    }
    gmoney =  await db.collection("jf_register").where({
      openid
    }).get()
    console.log(gmoney)
    console.log(gmoney.data[0])
    console.log(gmoney.data[0].money)
    var allmongey = price.data[0].price + gmoney.data[0].money
    return await db.collection("jf_register").where({
      openid,
    }).update({
      data:{
        money:allmongey
      }
    })

  }
  return  await db.collection("jf_info").where({
    openid,
    orderNum
  }).update({
    data:{
      state:1
    }
  })
}